<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page 
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber" 
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
        <Modal v-model="showAdd" width="700">
            <h3 slot="header" style="color:#2D8CF0">请填写退费原因</h3>
            <Row>
                <Col span="22" offset="1">
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title" style="height: 30px">
                            当前选中患者：<Tag v-if="formItem.patientName !== ''" color="blue">{{formItem.patientName}}</Tag>
                        </p>
                        <Input v-model="searchValue" placeholder="请通过病历号/手机号码检索" @keyup.enter.native="search"/>
                        <p style="color: #ff9900"><Icon type="information-circled"></Icon> 按回车键检索</p>
                    </Card>
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title">
                            退费或投诉原因：
                        </p>
                        <Form :label-width="120">
                            <FormItem label="医疗质量类：">
                                <RadioGroup v-model="formItem.medicalQuality">
                                    <Radio label="0">医疗技术不好</Radio>
                                    <Radio label="1">疗效不满意</Radio>
                                    <Radio label="2">医疗安全性没有充分告知</Radio>
                                    <Radio label="3">夸大疗效</Radio>
                                    <Radio label="4">沟通不客观或隐瞒</Radio>
                                    <Radio label="5">其他</Radio>
                                </RadioGroup>
                                <Input v-if="formItem.medicalQuality === '5'" v-model="formItem.supplement1" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="如果选择其他,请填写补充信息(非必填)"/>
                            </FormItem>
                            <FormItem label="服务类投诉：">
                                <RadioGroup v-model="formItem.customerService">
                                    <Radio label="0">服务态度（包括语言不友善，没有耐心）不好</Radio>
                                    <Radio label="1">服务质量（包括不及时回访，接待工作不好，接待工作不熟练等）不满意</Radio>
                                    <Radio label="2">医疗环境（包括卫生）不满意</Radio>
                                    <Radio label="3">推荐的项目设置不合理</Radio>
                                    <Radio label="4">服务流程不满意</Radio>
                                    <Radio label="5">注意事项告之不充分</Radio>
                                    <Radio label="6">对治疗没有信心</Radio>
                                    <Radio label="7">其他</Radio>
                                </RadioGroup>
                                <Input v-if="formItem.customerService === '7'" v-model="formItem.supplement2" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="如果选择其他,请填写补充信息(非必填)"/>
                            </FormItem>
                            <FormItem label="其他：">
                                <RadioGroup v-model="formItem.other">
                                    <Radio label="0">因故无法坚持疗程</Radio>
                                    <Radio label="1">家人坚持终止治疗</Radio>
                                    <Radio label="2">其他</Radio>
                                </RadioGroup>
                                <Input v-if="formItem.other === '2'" v-model="formItem.supplement3" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="如果选择其他,请填写补充信息(非必填)"/>
                            </FormItem>
                        </Form>
                    </Card>
                    <Form :label-width="120">
                        <FormItem label="您的要求：" prop="categoryId">
                            <RadioGroup v-model="formItem.claim">
                                <Radio label="0">退费/退还未消费的费用</Radio>
                                <Radio label="1">更换管理员</Radio>
                                <Radio label="2">其他</Radio>
                            </RadioGroup>
                            <Input v-if="formItem.claim === '2'" v-model="formItem.supplement4" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="如果选择其他,请填写补充信息(非必填)"/>
                        </FormItem>
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="info" size="large" @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            searchValue: '',
            showAdd: false,
            baseURL: '/user/refund',
            patientURL: '/user/patient',
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/refund',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 1700,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '投诉人',
                        key: 'patientName'
                    },{
                        title: '病历号',
                        key: 'medicalNumber'
                    },{
                        title: '医疗质量投诉内容',
                        key: 'medicalQuality',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.medicalQuality === 0) {
                                temp += '医疗技术不好'
                            } else if (params.row.medicalQuality === 1) {
                                temp += '疗效不满意'
                            } else if (params.row.medicalQuality === 2) {
                                temp += '医疗安全性没有充分告知'
                            } else if (params.row.medicalQuality === 3) {
                                temp += '夸大疗效'
                            } else if (params.row.medicalQuality === 4) {
                                temp += '沟通不客观或隐瞒'
                            } else if (params.row.medicalQuality === 5) {
                                temp += params.row.supplement1
                            }
                            return h('span', temp)
                        }
                    }, {
                        title: '服务质量投诉内容',
                        key: 'customerService',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.customerService === 0) {
                                temp += '服务态度（包括语言不友善，没有耐心）不好'
                            } else if (params.row.customerService === 1) {
                                temp += '服务质量（包括不及时回访，接待工作不好，接待工作不熟练等）不满意'
                            } else if (params.row.customerService === 2) {
                                temp += '医疗环境（包括卫生）不满意'
                            } else if (params.row.customerService === 3) {
                                temp += '推荐的项目设置不合理'
                            } else if (params.row.customerService === 4) {
                                temp += '服务流程不满意'
                            } else if (params.row.customerService === 5) {
                                temp += '注意事项告之不充分'
                            } else if (params.row.customerService === 6) {
                                temp += '对治疗没有信心'
                            } else if (params.row.customerService === 7) {
                                temp += params.row.supplement2
                            }
                            return h('span', temp)
                        }
                    }, {
                        title: '其他',
                        key: 'other',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.other === 0) {
                                temp += '因故无法坚持疗程'
                            } else if (params.row.other === 1) {
                                temp += '家人坚持终止治疗'
                            } else if (params.row.other === 2) {
                                temp += params.row.supplement3
                            }
                            return h('span', temp)
                        }
                    }, {
                        title: '投诉人要求',
                        key: 'claim',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.claim === 0) {
                                temp += '退费/退还未消费的费用'
                            } else if (params.row.claim === 1) {
                                temp += '更换管理员'
                            } else if (params.row.claim === 2) {
                                temp += params.row.supplement4
                            }
                            return h('span', temp)
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            formItem: {
                id: '',
                patientId: '',
                patientName: '',
                medicalNumber: '',
                medicalQuality: '',
                customerService: '',
                other: '',
                claim: '',
                supplement1: '',
                supplement2: '',
                supplement3: '',
                supplement4: ''
            }
        };
    },
    methods: {
        search () {
            if (this.searchValue !== '') {
                let url = this.patientURL + '/search'
                this.$ajax.post(url, this.addFilter(this.searchValue, 'phone', 'medical_number')).then(response => {
                    let {code, data} = response.data
                    if (code === 0) {
                        if (data.length > 0) {
                            if (data.length === 1) {
                                this.formItem.patientId = data[0].id
                                this.formItem.patientName = data[0].name
                                this.formItem.medicalNumber = data[0].medicalNumber
                            } else {
                                this.$bus.modal('warning', '系统提示', '查询异常,检索到多个患者信')
                            }
                        } else {
                            this.$bus.modal('warning', '系统提示', '未查询到患者信息')
                        }
                    }
                })
            } else {
                this.$bus.modal('warning', '系统提示', '请输入检索关键词')
            }
        },
        addFilter (val, ...fields) {
            let queryModel = {
                mode: 'or',
                pageNumber: 1,
                pageSize: 10,
                order: 'id',
                filters: []
            }
            for (let field of fields) {
                let filterModel = {'field': field, 'value1': val, 'operate': 'LIKE'}
                queryModel.filters.push(filterModel)
            }
            return queryModel
        },
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'print') {
                this.print()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            this.searchValue = ''
            this.$bus.init(this.formItem)
            this.showAdd = true
        },
        save () {
            console.log(this.formItem)
            if (this.formItem.patientId !== '') {
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '操作成功')
                        this.showAdd = false
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            } else {
                this.$bus.alert('warning', '请先选择患者')
            }
        },
        print () {
            if (this.selectRows.length === 1) {
                let url = this.$store.state.app.WEBROOT + '/user/print/printRefund?id=' + this.selectRows[0].id
                window.open(url)
            } else {
                this.$bus.alert('warning', '请选择一条记录')
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                if (ids.length === 1) {
                    this.doDelete(ids)
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                }
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            });
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/refund') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
